package com.wing.bill.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.wing.bill.service.WorkshopReportService;
import com.wing.cabinet.model.entity.UseInventoryInout;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.EnterpricceIdService;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Validated
@Api(value = "Admin-用料分析报表", tags = "#/bill/workshopReport")
@RestController("workshopReportController")
@RequestMapping("bill/admin/workshopReport")
@RequiredArgsConstructor
public class WorkshopReportController {

    private final WorkshopReportService workshopReportService;
    
    final String dateType = "before";

    @Value("${platform.gatewayUrl}")
    private String url;
    private final EnterpricceIdService enterpricceIdService;

    //顶部数据和点击
    @ApiOperation(value = "每个选项卡页面的顶部数据", notes = "查询")
    @GetMapping("/list")
    public JsonResult result(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("click")String click//顶部数据点击
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.materialDataAnalysis(type,startTime,endTime,click,code,mainCabCode);
    }

    @ApiOperation(value = "领料趋势变化图")
    @GetMapping("/findMaterialRequisitionTrend")
    public JsonResult findMaterialRequisitionTrend(
            @RequestParam("param")String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
            ,@RequestParam("mainCabCode") String mainCabCode //主柜选择
    ){ return workshopReportService.findMaterialRequisitionTrend(param,JwtUtils.getEnterpriseCode(),mainCabCode);}

    @ApiOperation(value = "领料时间对比图")
    @GetMapping("/findMaterialTimeComparison")
    public JsonResult findMaterialTimeComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findMaterialTimeComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "设备领料对比图（TOP10）")
    @GetMapping("/findEquipmentMaterialComparison")
    public JsonResult findEquipmentMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findEquipmentMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "人员领料对比图（TOP10)")
    @GetMapping("/findPersonnelMaterialComparison")
    public JsonResult findPersonnelMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findPersonnelMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "工单领料对比图（TOP10)")
    @GetMapping("/findWorkOrderMaterialComparison")
    public JsonResult findWorkOrderMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findWorkOrderMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "车间领料对比图")
    @GetMapping("/findWorkshopMaterialComparison")
    public JsonResult findWorkshopMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findWorkshopMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "供应商物料消耗对比")
    @GetMapping("/findSupplierMaterialComparison")
    public JsonResult findSupplierMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findSupplierMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "产品领料对比图")
    @GetMapping("/findProductMaterialComparison")
    public JsonResult findProductMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findProductMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "领料品牌对比图")
    @GetMapping("/findBrandMaterialComparison")
    public JsonResult findBrandMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findBrandMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "物料消耗智能柜对比")
    @GetMapping("/findCabinetMaterialComparison")
    public JsonResult findCabinetMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findCabinetMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "领料物料分类对比")
    @GetMapping("/findCategoryMaterialComparison")
    public JsonResult findCategoryMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ) {
        String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findCategoryMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }


    @ApiOperation(value = "异常登记人员对比")
    @GetMapping("/findAbnormalRegistrationComparison")
    public JsonResult findAbnormalRegistrationComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findAbnormalRegistrationComparison(type,dateType,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "异常原因占比分析")
    @GetMapping("/findAbnormalCauseAnalysis")
    public JsonResult findAbnormalCauseAnalysis(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findAbnormalCauseAnalysis(type,dateType,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "领新物料占比")
    @GetMapping("/findGetNewMaterialComparison")
    public JsonResult findGetNewMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findGetNewMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "未归还物料占比")
    @GetMapping("/findReturnedMaterialComparison")
    public JsonResult findReturnedMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findReturnedMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "暂存物料占比")
    @GetMapping("/findTSMaterialComparison")
    public JsonResult findTSMaterialComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findTSMaterialComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "采购物料规格型号占比")
    @GetMapping("/findPurchaseMaterialModelComparison")
    public JsonResult findPurchaseMaterialModelComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findPurchaseMaterialModelComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "采购物料供应商占比分析")
    @GetMapping("/findPurchaseMaterialSupplierComparison")
    public JsonResult findPurchaseMaterialSupplierComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findPurchaseMaterialSupplierComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "暂存物料品牌占比分析")
    @GetMapping("/findTSMaterialBrandComparison")
    public JsonResult findTSMaterialBrandComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findTSMaterialBrandComparison(type,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "报废物料规格型号占比")
    @GetMapping("/findCheckedResultMaterialModelComparison")
    public JsonResult findScrapMaterialModelComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findCheckedResultMaterialModelComparison(type,dateType,startTime,endTime,code,mainCabCode,param,3);
    }
    @ApiOperation(value = "报修物料规格型号占比")
    @GetMapping("/findRepairsMaterialModelComparison")
    public JsonResult findRepairsMaterialModelComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findCheckedResultMaterialModelComparison(type,dateType,startTime,endTime,code,mainCabCode,param,2);
    }
    @ApiOperation(value = "报失物料规格型号占比")
    @GetMapping("/findMissMaterialModelComparison")
    public JsonResult findMissMaterialModelComparison(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
        //String code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findLostResultMaterialModelComparison(type,dateType,startTime,endTime,code,mainCabCode,param, UseInventoryInout.Type.loss.getValue());
    }
    @ApiOperation(value = "成本中心")
    @GetMapping("/findMissMaterialCostCenter")
    public JsonResult findMissMaterialCostCenter(
            @RequestParam("type")String type//012 日周月
            ,@RequestParam("startTime")String startTime//时间搜索 开始时间
            ,@RequestParam("endTime")String endTime//时间搜索 结束时间
            ,@RequestParam("mainCabCode")String mainCabCode //主柜编码
            ,@RequestParam("code")String code //企业编码
            ,@RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){
         code = JwtUtils.getEnterpriseCode();
        return workshopReportService.findMissMaterialCostCenter(type,dateType,startTime,endTime,code,mainCabCode,param);
    }

    @ApiOperation(value = "升级报告")
    @GetMapping("/getUpgradeReport")
    public JsonResult getUpgradeReport(){
        if (url.endsWith("/")){
            url=url.substring(0,url.length());
        }
        String channelCode = null;
        EnterpricceIdVO enterpricceIdVO=enterpricceIdService.getEnterpricceIdVO();
        if (enterpricceIdVO!=null){
            channelCode=enterpricceIdVO.getChannelCode();
        }

        JsonResult data = null;
        try {
            String s= HttpHelper.httpGet(url + "/supplier/cabinet/admin/upgradeReport/getUpgradeReport?channelCode="+ channelCode);
            data = JSONObject.parseObject(s, JsonResult.class);
        } catch (Exception e) {
            log.error("升级报告查询异常:",e);
            return JsonResult.error(555,"查询异常："+e.getMessage());
        }

        return data;
    }
}
